/**
 * MainMenu.java
 * @author Vincent Dominguez
 * 
 * The main menu for the Minesweeper Flags application
 */

package com.vmd.MSFlags;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class MainMenu extends ListActivity {
	/* Private Fields */
	private static enum MenuItems { MENU_START, MENU_HOWTO, MENU_SOUND }
	private MediaPlayer startClip;
	private MainMenuAdapter<String> adapter;
	private boolean soundOn;
	
	/* Default Fields */
	static OnCompletionListener listener = new OnCompletionListener() {
		@Override
		public void onCompletion(MediaPlayer mp) {
			mp.release();
		}
    };
	
    /* Overridden Life Cycle Methods */
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_menu);
        
        // initialize sound-related fields and objects
        SharedPreferences prefs = getSharedPreferences(getString(R.string.preferences_file), MODE_PRIVATE);
        soundOn = prefs.getBoolean(getString(R.string.sound), true);
        
        // create an ListAdapter for the main menu list
        adapter = new MainMenuAdapter<String>(this, R.layout.list_item,
        		R.id.list_text, getResources().getStringArray(R.array.main_options));
        setListAdapter(adapter);
    }
    
    @Override
    public void onPause() {
    	super.onPause();
    	
    	if (startClip.isPlaying()) {
    		// if the clip is playing, attach an onCompletionListener to it
        	// so it will release itself when its done
    		startClip.setOnCompletionListener(listener);
    	} else {
    		// if the clip isn't playing, just release it right away
    		startClip.release();
    	}
    	
    	// save sound setting
    	Editor editPrefs = getSharedPreferences(getString(R.string.preferences_file), MODE_PRIVATE).edit();
    	editPrefs.putBoolean(getString(R.string.sound), soundOn);
    	editPrefs.commit();
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	
    	// initialize media player
    	startClip = MediaPlayer.create(this, R.raw.start);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (intent != null) {
        	boolean returnedSoundValue = intent.getBooleanExtra(getString(R.string.sound), true);
        	soundOn = returnedSoundValue;
        	adapter.notifyDataSetChanged();
        }
    }
    
    /* UI Handlers */
	@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	// if the user is changing the sound setting, we'll handle playing the clip
    	// in another method
    	if ((MenuItems.values()[position] != MenuItems.MENU_SOUND) && soundOn) {
    		startClip.start();
    	}
    	// determine what to do based on which option was clicked
    	switch(MenuItems.values()[position]) {
    	// start a new game
    	case MENU_START:
    		Intent i = new Intent(this, MinesweeperFlags.class);
    		i.putExtra(getString(R.string.sound), soundOn);
        	startActivityForResult(i, 0);
        	break;
    	case MENU_HOWTO:
    		i = new Intent(this, HowTo.class);
    		startActivity(i);
    		break;
    	case MENU_SOUND:
    		switchSound();
    		break;
    	}
    }
	
	/* Private Helpers */
	/**
	 * switches sound from on to off and off to on
	 */
	private void switchSound() {
		if (soundOn) {
			soundOn = false;
		} else {
			soundOn = true;
			startClip.start();
		}
		adapter.notifyDataSetChanged();
	}

	/* Embedded Classes */
	/**
	 * MainMenuAdapter
	 * Extension of ArrayAdapter to assist with dynamically displaying
	 * the status of the sound setting
	 */
	class MainMenuAdapter<T> extends ArrayAdapter<T> {
		
		/**
		 * Constructor
		 */
		MainMenuAdapter(Context ctx, int resource, int textViewResourceId, T[] items) {
			super(ctx, resource, textViewResourceId, items);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View menuItem = convertView;
			
			// if this is not a recycled view, create a new one
			if (menuItem == null) {
				LayoutInflater inflater = getLayoutInflater();
				menuItem = inflater.inflate(R.layout.list_item,	parent, false);
			}
			
			// set the text for the item
			TextView text = (TextView) menuItem.findViewById(R.id.list_text);
			text.setText(getItem(position).toString());
			
			// adjust the item to display the current sound setting
			if (position == MenuItems.MENU_SOUND.ordinal()) {
				text.setText(text.getText() + (soundOn ? " On" : " Off"));
			}
			
			return menuItem;
		}
	}
}